﻿using System;
using System.Windows.Forms;

namespace 物流管理系统
{
    public partial class Consignmentinfo : Form
    {
        DBOperation dboperation = new DBOperation();
        public int formtype;
        ReMatch re = new ReMatch();
        public Consignmentinfo()
        {
            InitializeComponent();
        }

        private void confirm_Click(object sender, EventArgs e)
        {
            foreach (Control c in Controls)
            {
                if (c is TextBox txt)
                {
                    if (txt.Text==""&&(!c.Name.Equals("remarkstext")))
                    {
                        MessageBox.Show("有重要项目为空，请检查！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                if (c is ComboBox cmb)
                {
                    if (cmb.Text == "")
                    {
                        MessageBox.Show("有重要项目为空，请检查！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
            if (!re.idmatch(idtext.Text))
            {
                MessageBox.Show("编号输入非法！！！\n格式为英文和数字组成的5-15位字符串。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!re.intmatch(quantitytext.Text))
            {
                MessageBox.Show("托运件数输入非法！！！\n格式为0以上自然数。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!(re.intmatch(weighttext.Text)||re.decmatch(weighttext.Text)))
            {
                MessageBox.Show("托运重量输入非法！！！\n格式为整数或最多2位小数的数字。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!(re.intmatch(transpricetext.Text)) || re.decmatch(transpricetext.Text))
            {
                MessageBox.Show("货物运价输入非法！！！\n格式为整数或最多2位小数的数字。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!re.adressmatch(startpointtext.Text))
            {
                MessageBox.Show("发货地输入非法！！！\n格式为中英文和数字组成的2位以上地址。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!re.adressmatch(destinationtext.Text))
            {
                MessageBox.Show("收货地输入非法！！！\n格式为中英文和数字组成的2位以上地址。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!re.namematch(receivertext.Text))
            {
                MessageBox.Show("收货人输入非法！！！\n格式为中文2-10位或英文2-20位的姓名。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!re.phonematch(receiverphonetext.Text))
            {
                MessageBox.Show("收货人联系电话输入非法！！！\n格式为国内11位号码或者+开头的国际11位号码。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (re.specialmatch(remarkstext.Text))
            {
                MessageBox.Show("请勿随意改写备注！！！\n备注中不能包含有某些特殊符号。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (formtype)
            {
                case 0://添加
                    {
                        if (dboperation.exists("select "+idlabel.Text+" from "+Text+"表 where "+idlabel.Text+"='"+idtext.Text+"'", idtext.Text))
                        {
                            MessageBox.Show("此编号已经存在！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        else
                        {
                            dboperation.nonquerysql("insert into 托运信息表 VALUES('"+idtext.Text+"','"+consignmentdatepicker.Value+"','"+quantitytext.Text+"','"+weighttext.Text+"','"+goodsidcombobox.Text+"','"+transpricetext.Text+"','"+costtext.Text+"','"+customeridtext.Text+"','"+startpointtext.Text+"','"+destinationtext.Text+"','"+receivertext.Text+"','"+receiverphonetext.Text+"','"+statecombobox.Text+"','"+remarkstext.Text+"');");
                        }
                        //更新
                        dboperation.nonquerysql("update 托运信息表 set 托运状态='未装车',备注=concat(备注,'"+DateTime.Now.ToString()+"：登记；') where 托运编号='"+idtext.Text+"';");
                        break;
                    }
                case 1://修改
                    {
                        dboperation.nonquerysql("replace into 托运信息表 VALUES('"+idtext.Text+"','"+consignmentdatepicker.Value+"','"+quantitytext.Text+"','"+weighttext.Text+"','"+goodsidcombobox.Text+"','"+transpricetext.Text+"','"+costtext.Text+"','"+customeridtext.Text+"','"+startpointtext.Text+"','"+destinationtext.Text+"','"+receivertext.Text+"','"+receiverphonetext.Text+"','"+statecombobox.Text+"','"+remarkstext.Text+"');");
                        break;
                    }
                case 2://查看
                    {
                        Close();
                        break;
                    }
            }
            Close();
        }

        private void Consignmentregisterform_Load(object sender, EventArgs e)
        {
            switch (formtype)
            {
                case 0://添加
                    {
                        break;
                    }
                case 1://修改
                    {
                        idtext.Enabled=false;
                        break;
                    }
                case 2://查看
                    {
                        foreach (Control c in Controls)
                        {
                            if (!(c is Button))
                            {
                                c.Enabled=false;
                            }
                        }
                        costcacu.Enabled=false;
                        break;
                    }
            }
        }

        private void costcacu_Click(object sender, EventArgs e)
        {
            try
            {
                double discount = 0.1*dboperation.numberreturn("select 折扣 from 会员类型信息表 where 会员类型名=(select 所属会员类型 from 客户信息表 where 客户编号='"+customeridtext.Text+"')");
                double weight = double.Parse(weighttext.Text);
                double transprice = double.Parse(transpricetext.Text);
                costtext.Text=""+Math.Round(weight*transprice*discount, 2);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("重量和运价格式有误！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

        }
    }
}
